package com.jwdf.uda.ui.fragment.question;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.databinding.FragmentQuestionDynamicBinding;
import com.jwdf.uda.event.IndividualEvent;
import com.jwdf.uda.event.UserEvent;
import com.jwdf.uda.event.UserInfoRefreshEvent;
import com.jwdf.uda.event.UserInfoShowEvent;
import com.jwdf.uda.event.UserShowEvent;
import com.jwdf.uda.http.AHttpService;
import com.jwdf.uda.http.HttpHelper;
import com.jwdf.uda.http.action.QuizIndexAction;
import com.jwdf.uda.ui.adapter.QuestionRecyclerViewAdapter;
import com.jwdf.uda.ui.fragment.base.BaseFragment;
import com.jwdf.uda.utils.ConfigUtils;
import com.jwdf.uda.utils.EventBusUtil;
import com.jwdf.uda.view.recyclerView.PullLoadMoreRecyclerView;
import retrofit2.Response;

/**
 * Created by Administrator on 2016/9/26.
 */
public class QuestionDynamicFragment extends BaseFragment {
    public boolean mPaddingBottom = true;
    protected String mStyleTag = "";
    boolean mRefresh;
    PbBean.PbResQuestionQuestionList mBoy;
    private QuestionRecyclerViewAdapter mQuestionRecyclerViewAdapter;
    private FragmentQuestionDynamicBinding mDataBinding;

    public static QuestionDynamicFragment instance() {
        QuestionDynamicFragment view = new QuestionDynamicFragment();
        return view;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mDataBinding = FragmentQuestionDynamicBinding.inflate(inflater, container, false);
        initViews();
        requestData(true);
        return mDataBinding.getRoot();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mDataBinding.setQuestionDynamicFragment(this);
    }

    private void initViews() {
        mPaddingBottom = !TextUtils.equals(getUserId(), ConfigUtils.getUserId(getContext()));
        mStyleTag = getArguments().getString("style", "");
        mQuestionRecyclerViewAdapter = new QuestionRecyclerViewAdapter(getActivity());
        mDataBinding.pullLoadMoreRecyclerView.setLinearLayout();
        mDataBinding.pullLoadMoreRecyclerView.setRefreshing(true);
        mDataBinding.pullLoadMoreRecyclerView.addHeaderView(createHeaderView());
        mDataBinding.pullLoadMoreRecyclerView.setAdapter(mQuestionRecyclerViewAdapter);
        mDataBinding.pullLoadMoreRecyclerView.setPullRefreshEnable(false);
        mDataBinding.pullLoadMoreRecyclerView.setOnPullLoadMoreListener(new PullLoadMoreRecyclerView.PullLoadMoreListener() {
            @Override
            public void onRefresh() {
                mStyleTag = "";
                requestData(true);
            }

            @Override
            public void onLoadMore() {
                requestData(false);
            }
        });

    }

    private View createHeaderView() {
        View headerView = getActivity().getLayoutInflater().inflate(R.layout.view_head_divider, null);
        return headerView;
    }

    public void autoRefresh() {
        mStyleTag = "";
        requestData(true);
//        mIXListener.onRefresh(mDataBinding.showFragmentList);
    }
    private void requestData(final boolean refresh) {
        if (refresh && mQuestionRecyclerViewAdapter.isEmpty()) {
//            showProgressDialog();
        }
        int offset = refresh ? 0 : mQuestionRecyclerViewAdapter.getCount();
        onNewRequestCall(QuizIndexAction.newInstance(getContext(),offset,0, getUserId(), PbBean.PbSignBaseQuestionType.QUESTION_INDEX_NONE).request(new AHttpService.IResCallback<PbBean.PbResQuestionQuestionList>() {
            @Override
            public void onCallback(int resultCode, Response<PbBean.PbResQuestionQuestionList> response, PbBean.PbBaseServerInfo baseServerInfo) {
                if (getView() != null) {
//                    closeProgressDialog();
                    if (HttpHelper.showError(getContext(), resultCode, baseServerInfo, getString(R.string.error_net))) {
                        mDataBinding.pullLoadMoreRecyclerView.setPullLoadMoreCompleted();
                        EventBusUtil.post(new UserInfoRefreshEvent());
                        return;
                    }
                    doSuccessResponse(refresh, response.body());
                }
            }
        }));

    }
    protected String getUserId() {
        return ConfigUtils.getUserId(getContext());
    }

    private void doSuccessResponse(final boolean refresh, PbBean.PbResQuestionQuestionList body) {
        mRefresh=refresh;
        mBoy=body;
        if (mBoy != null) {
            if (mRefresh) {
                mQuestionRecyclerViewAdapter.setData(mBoy.getQuestionInfoList());
            } else {
                mQuestionRecyclerViewAdapter.append(mBoy.getQuestionInfoList());
            }
            mQuestionRecyclerViewAdapter.notifyDataSetChanged(mBoy.getQuestionInfoList(),mRefresh);
            mDataBinding.pullLoadMoreRecyclerView.setPullLoadMoreCompleted();
            mDataBinding.llUserShowEmpty.setVisibility(View.GONE);
            mDataBinding.tvEmptyData.setVisibility(View.GONE);
            mDataBinding.pullLoadMoreRecyclerView.setNoData(body.getQuestionInfoList().size()==getPageLength(),body.getQuestionInfoList().size(),getPageLength());
//            if (mQuestionRecyclerViewAdapter.getCount() == 0) {
//                if (TextUtils.equals(getUserId(), ConfigUtils.getUserId(getContext()))) {
//                    mDataBinding.llUserShowEmpty.setVisibility(View.VISIBLE);
//                }else {
//                    mDataBinding.tvEmptyData.setVisibility(View.VISIBLE);
//                }
//            }
            EventBusUtil.post(new UserInfoRefreshEvent());
        }
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBusUtil.registerEvent(this);
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBusUtil.unregisterEvent(this);

    }
    public void onEvent(UserShowEvent event) {
        requestData(true);
    }
    public void onEventMainThread(UserEvent event) {
        if(UserEvent.EVENT_ADD_SHOW_SUCCESS == event.getEvent()) {
            mStyleTag = "";
            requestData(true);
        }
    }
    public void onEventMainThread(UserInfoShowEvent event) {
        mStyleTag = "";
        requestData(true);
    }

    public void onEvent(IndividualEvent event) {
        requestData(true);
    }

}
